#include "client.h"

char                    *my_strcat(char *s1,
                                   char *s2)
{
  char                  *new = NULL;
  unsigned int          i = 0;
  unsigned int          j = 0;

  new = malloc(strlen(s1) + strlen(s2) * sizeof (char));
  while (i < strlen(s1))
    {
      new[i] = s1[i];
      i++;
    }
  while (j < strlen(s2))
    {
      new[i] = s2[j];
      i++;
      j++;
    }
  new[i] = '\0';
  return (new);
}


void send_file(char* filename, int sockfd, struct sockaddr_in fsock, int size)
{
  FILE *stream;
  char *buf = malloc(1024);
  char *newbuf;
  char *tmp;
  char *interim = malloc(2);
  interim[0] = '1';
  interim[1] = '0';
  char *filename_new = malloc(strlen(filename) + 1);

  //  printf("Le nom du fichier est : %s\n",filename);
  stream = fopen(filename, "rw");
  if (stream != NULL)
    {
      memset(buf, '\0', 1024);

      buf[0] = '1';
      buf[1] = '0';
      while (fread((buf + strlen(filename) + 3), 1, 1021 - strlen(filename)
		   , stream) > 0)
	{
	  int j = 2;
	  int i = 0;
	  for(i; i < strlen(filename); i++)
	    {
	      buf[j] = filename[i];
	      j++;
	    }
	  //	  buf[j] = '\'';
	  char *save = malloc(1024);
	  strcpy(save, buf + strlen(filename) + 3);
	  strcat(buf, "\'");
	  strcat(buf, save);
	  printf("buffer : %s\n", buf);
	  /*	  strcat(filename_new, filename);
	  tmp = malloc(strlen(filename_new) + strlen(interim));
	  memset(tmp, '\0', strlen(tmp));
	  strcat(tmp, interim);
	  strcat(tmp, filename_new);
	  newbuf = malloc(strlen(tmp) + strlen(buf));
	  strcat(newbuf, tmp);
	  strcat(newbuf, buf);
	  printf("newbuf:%s\n", newbuf); */
	  if (sendto(sockfd, buf, strlen(buf),
		     0, (struct sockaddr *) &fsock, size) < 0)
	    {
	      perror("sendto()");
	      exit(errno);
	    }

	  memset(buf, '\0', 1024);
	  //	  free(tmp);
	  // free(newbuf);
	}
      char *end = "-1\0";
      sendto(sockfd, end, strlen(end), 0, (struct sockaddr *) &fsock, size);
      fclose(stream);
    }
  else
    {
    printf("STREAM NULL");
    perror("fopen()");
    //    exit(errno);
    }
}

void read_dir(int sockfd, struct sockaddr_in fsock, int size)
{
  DIR *direct = opendir(CURRENT_DIR);

  if (direct == NULL)
    {
      printf("invalid Directory\n");
      exit(1);
    }
  struct dirent *content = NULL;

  while (1)
    {
      content = readdir(direct);
      if (content == NULL)
	{
	  direct = opendir(CURRENT_DIR);
	  sleep(1);
	  continue;
	}
      if ((strcmp(content->d_name, "..") == 0)
	|| (strcmp(content->d_name, ".")) == 0)
	continue;
      //      printf("Hello world!");

      //      printf("%s\n",my_strcat(CURRENT_DIR, content->d_name));
      char *filename = malloc(strlen(CURRENT_DIR) + strlen(content->d_name));
      memset(filename, '\0', strlen(filename));
      strcat(filename, CURRENT_DIR);
      strcat(filename, content->d_name);
      printf("FICHIER : %s\n", filename);
      send_file(filename, sockfd, fsock, size);
      //      remove(my_strcat(CURRENT_DIR, content->d_name));
      sleep(1);
    }
}


void client_unicast(char *DestIp, int DestPort)
{
  struct sockaddr_in fsock = {0};
  struct sockaddr_in ssock = {0};
  int	sockfd;
  char *res = NULL;
  char *new_mess = NULL;
  char buf[BUFFSIZE] = {0};
  char msg[2048];
  int size = sizeof(fsock);
  int i = 0;
  int j = 0;
  int len = 0;

  struct hostent *hostinfo = NULL;

  res = malloc(18 * sizeof(char));
  new_mess = malloc(18 * sizeof(char));

  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sockfd == -1)
    {
      perror("socket()");
      exit(errno);
    }

  //  fsock = malloc(sizeof(struct sockaddr_in));
  fsock.sin_family = AF_INET;
  fsock.sin_port = htons(DestPort);

  if (inet_addr(DestIp) == -1)
    printf("cest la merde");

  new_mess = strcat(new_mess, "20");
  new_mess = strcat(new_mess, get_groupip(get_ip()));

  for (i = 2; ((new_mess[i] != '\0') && i < (strlen(new_mess))); i++)
    {
      res[j] = new_mess[i];
      j++;
    }
  j = 0;

  ssock.sin_addr.s_addr = inet_addr(res);
  ssock.sin_family = AF_INET;
  ssock.sin_port = htons(DestPort);
  len = sizeof(ssock);
  res = NULL;
  res = malloc(18 * sizeof(char));
  

  if (sendto(sockfd, new_mess, strlen(new_mess), 0, (struct sockaddr *) &ssock,
	     sizeof(ssock)) < 0)
    {
      perror("sendto2");
    }

  if (recvfrom(sockfd, buf, 1024, 0, (struct sockaddr *) &ssock, &len) < 0)
    {
      perror("recvfrom error");
    }

  for (i = 2; ((buf[i] != '\0') && i < (strlen (buf))); i++)
    {
      res[j] = buf[i];
      j++;
    }

  fsock.sin_addr.s_addr = inet_addr(res);

  read_dir(sockfd, fsock, size);
  close(sockfd);
}
